﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using FrmMain;

namespace FMain
{
    public partial class FrmDgv2 : Form
    {
        public FrmDgv2()
        {
            InitializeComponent();
        }
        Dao dao = new Dao();
        private void FrmDgv2_Load(object sender, EventArgs e)
        {
            this.dataGridView1.DataSource = add();
            

        }

        public DataTable add()
        {
            string sql = "select * from food";
            return dao.GetTable(sql);
        }

        private void 新增ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            FrmAdd add = new FrmAdd();
            add.frd = this;
            
            add.Show();
        }

        private void 删除ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (this.dataGridView1.SelectedRows.Count > 0) {
                int id = Convert.ToInt32(dataGridView1.SelectedRows[0].Cells[0].Value);
                string sql = "delete  from food  where Fid = " + id;
                if (dao.DoNonquery(sql) > 0) {
                    MessageBox.Show("删除成功");
                    FrmDgv2_Load(null, null);
                }
                else
                {
                    MessageBox.Show("删除失败");

                }
            }
        }

        private void 点赞ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (this.dataGridView1.SelectedRows.Count > 0)
            {
                int id = Convert.ToInt32(dataGridView1.SelectedRows[0].Cells[0].Value);
                string sql = "Update food set Fzan = fzan+1   where Fid = " + id;
                if (dao.DoNonquery(sql) > 0)
                {
                    MessageBox.Show("点赞成功");
                    FrmDgv2_Load(null, null);
                }
                else
                {
                    MessageBox.Show("点赞失败");

                }
            }
        }
    }
}
